import axios from "axios";
import type { AxiosInstance, AxiosResponse, AxiosError, InternalAxiosRequestConfig } from 'axios';

const request: AxiosInstance = axios.create({
  baseURL: import.meta.env.VIT_BASE_API,
  timeout: 10000
})

request.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  return config
}, (err: AxiosError) => {
    return Promise.reject(err);

})

request.interceptors.response.use(
  (response: AxiosResponse) => {
    return response
  },
  // 请求失败
  (error: any) => {
    let errorMessage;
    if (error.response && error.response.data && error.response.data.message) {
      errorMessage = error.response.data.message;
    }
    return Promise.reject(errorMessage);
  },
);

export default request